from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
# Create your views here.
from myapp.ser import NavSerializer,BannerSerializer
from myapp.models import NavModel,BannerModel
from django.core.cache import cache


class TopView(APIView):
    def get(self,request):
        key = 'topnav'
        cacheres=cache.get(key)
        if cacheres:
            return Response({'msg': 'succcess', 'data': cacheres, 'code': 200})
        data=NavModel.objects.filter(position=True,ishttp=True).order_by('order')
        ser=NavSerializer(data,many=True)
        cache.set(key, ser.data, 60 * 60 * 24)
        return Response({'msg':'success','data':ser.data,'code':200})

class FootView(APIView):
    def get(self,request):
        key = 'footnav'
        cacheres=cache.get(key)
        if cacheres:
            return Response({'msg':'succcess','data':cacheres,'code':200})
        data=NavModel.objects.filter(position=False,ishttp=True).order_by('order')
        ser=NavSerializer(data,many=True)
        cache.set(key,ser.data,60*60*24)
        return Response({'msg':'success','data':ser.data,'code':200})

class Banner(APIView):
    def get(self,request):
        key = 'banner'
        cacheres=cache.get(key)
        if cacheres:
            return Response({'msg': 'succcess', 'data': cacheres, 'code': 200})
        banner=BannerModel.objects.filter(isshow=True).order_by('order')
        ser=BannerSerializer(banner,many=True)
        cache.set(key, ser.data, 60 * 60 * 24)
        return Response({'msg':'success','data':ser.data,'code':200})